
#pragma once

// -----------------------------------------------------------------------------

#include "Win32.Diagnostics.Log.h"
#include "Win32.Memory.h"
#include "Win32.IO.TextFile.h"

namespace Kif
{
namespace Win32
{
namespace Drawing
{

class Vectors
{
private:
   long maxSize;

   inline void CopyAxis(double *d, long dLength, double *s, long sLength)
   {
      long i,t = sLength > dLength ? dLength : sLength;
      for (i=0; i < t; i++)
      {
         *d = *s;
         d++;
         s++;
      }
   }  // end of CopyAxis

public:

   struct XmlTags
   {
      TCHAR *VectorSets;
      TCHAR *VectorSet;

      XmlTags()
      {
         VectorSets = L"vectorsets";
         VectorSet  = L"vectorset"; 
      }
   }  ;  // end of XmlTags

   XmlTags Tags;

   static const int CommandMove = 0;
   static const int CommandDraw = 1;

   Kif::Win32::IO::TextFile *pOutputFile;

   long Count;
   int *command;
   double *x;
   double *y;
   double *z;
   double Tolerance;

   Vectors()
   {
      Count = maxSize = 0;
      x = NULL;
      y = NULL;
      z = NULL;
      Tolerance = 0.001;
   }  // end of Vectors (ctor)

  ~Vectors()
   {
      if (pOutputFile)
         FileClose();

      if (x != NULL)
         delete x;
      if (y != NULL)
         delete y;
      if (z != NULL)
         delete z;

      x = y = z = NULL;
   }  // end of Vectors (dtor)

   bool Alloc(long size);
   long Add(double x1, double y1, double z1,
            double x2, double y2, double z2);
   long Add(double x1, double y1, double z1);

   // support for writing vectors
   bool FileOpen(TCHAR *pFilePathName);
   void FileWrite(TCHAR *pVectorSetName);
   void FileWrite(char *pVectorSetName)
   {
      Kif::Win32::String fn;
      fn.Copy(pVectorSetName);
      FileWrite(fn.GetBuffer());
   }
   void FileClose();

   /// <summary>Compare two points with given tolerance</summary>
   /// <param name="p1">point 1</param>
   /// <param name="p2">point 2</param>
   /// <param name="tolerance">tolerance</param>
   /// <returns>true is returned if vector/s were allocated</returns>
   inline bool ComparePoints(double p1, double p2, double tolerance)
   {
      return(((p1 - tolerance) <= p2) && ((p1 + tolerance) >= p2));
   }
   inline bool ComparePoints(
      double x1, double y1, double z1,
      double x2, double y2, double z2, double tolerance)
   {
      return(ComparePoints(x1,x2,tolerance) &&
             ComparePoints(y1,y2,tolerance) &&
             ComparePoints(z1,z2,tolerance));
   }

}  ;  // end of Vectors

}  // end of Kif.Drawing
}  // end of Kif.Win32
}  // end of Kif namespace

